package com.bbm.communitysystem.controller.post;

import cn.hutool.core.bean.BeanUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.domain.Post;
import com.bbm.communitysystem.domain.UserInfo;
import com.bbm.communitysystem.domain.dto.post.PostMessageDto;
import com.bbm.communitysystem.domain.vo.*;
import com.bbm.communitysystem.service.ILikePostService;
import com.bbm.communitysystem.service.IPostService;
import com.bbm.communitysystem.service.IUserInfoService;
import com.bbm.communitysystem.service.impl.CommentServiceImpl;
import com.bbm.communitysystem.service.impl.OssFileServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "帖子管理")
@RestController
@RequestMapping("/post/manage")
@RequiredArgsConstructor
public class PostManageController {

    private final IPostService postService;

    private final IUserInfoService userInfoService;

    private final OssFileServiceImpl ossFileService;

    private final ILikePostService likePostService;

    private final CommentServiceImpl commentService;



    // 获取待审核帖子列表
    @Operation(summary = "审核员获取未发布的帖子列表")
    @GetMapping("/userPost")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "size", description = "每页数量")
    })
    public R<PageVo<PostVo>> getUserPostByNoPublic(
            @RequestParam("page") Integer page,
            @RequestParam("size") Integer size) {

        Page<Post> postPage = new Page<>(page, size);
        postService.lambdaQuery()
                .in(Post::getStatus, 3) // 查询编辑中和审核中的帖子
                .orderByDesc(Post::getUpdateTime)
                .page(postPage);

        PageVo<PostVo> pageVo = new PageVo<>(postPage);

        postPage.getRecords().forEach(post -> {
            PostVo vo = new PostVo();
            // 基础信息复制
            BeanUtil.copyProperties(post, vo);

            // 设置用户信息
            UserInfo user = userInfoService.getById(post.getUserId());
            vo.setAvatar(user.getUserImg());
            vo.setNickName(user.getUserNick());

            // 处理附件文件
            List<OssFileVo> files = new ArrayList<>();
            if (post.getOssId() != null) {
                Arrays.stream(post.getOssId().split(","))
                        .map(ossFileService::getById)
                        .filter(Objects::nonNull)
                        .forEach(ossFile -> {
                            OssFileVo fileVo = new OssFileVo();
                            BeanUtil.copyProperties(ossFile, fileVo);
                            fileVo.setType(ossFile.getFileType());
                            files.add(fileVo);
                        });
            }
            vo.setFile(files);

            pageVo.getRecords().add(vo);
        });

        return R.ok("获取成功", pageVo);
    }

    // 获取帖子详情
    @Operation(summary = "审核员获取帖子详情")
    @GetMapping("/{id}")
    public R<PostDeVo> getPostById(@PathVariable("id") Integer id) {
        Post post = postService.getById(id);
        if (post == null) {
            return R.error("帖子不存在");
        }

        PostDeVo vo = new PostDeVo();
        // 复制基础属性
        BeanUtil.copyProperties(post, vo);

        // 设置用户信息
        UserInfo user = userInfoService.getById(post.getUserId());
        vo.setAvatar(user.getUserImg());
        vo.setNickName(user.getUserNick());

        // 设置附件文件
        List<OssFileVo> files = new ArrayList<>();
        if (post.getOssId() != null) {
            Arrays.stream(post.getOssId().split(","))
                    .map(ossFileService::getById)
                    .filter(Objects::nonNull)
                    .forEach(ossFile -> {
                        OssFileVo fileVo = new OssFileVo();
                        BeanUtil.copyProperties(ossFile, fileVo);
                        fileVo.setType(ossFile.getFileType());
                        files.add(fileVo);
                    });
        }
        vo.setFile(files);


        return R.ok("获取成功", vo);
    }

    // 审核通过
    @Operation(summary = "审核员通过")
    @PostMapping("/success")
    public R<String> PostSuccess(@RequestBody PostMessageDto messageDto) {
        boolean success = postService.lambdaUpdate()
                .eq(Post::getId, messageDto.getPostId())
                .set(Post::getStatus, 1) // 审核成功状态
                .set(Post::getPublicTime, System.currentTimeMillis() + "")
                .set(Post::getLabel, messageDto.getLabel())
                .update();

        return success ?
                R.ok("审核通过") :
                R.error("审核操作失败");
    }

    // 审核不通过
    @Operation(summary = "审核员不通过")
    @PostMapping("/fail")
    public R<String> PostFail(@RequestBody PostMessageDto messageDto) {
        boolean success = postService.lambdaUpdate()
                .eq(Post::getId, messageDto.getPostId())
                .set(Post::getStatus, 4) // 审核失败状态
                .set(Post::getMessage, messageDto.getMessage())
                .update();

        return success ?
                R.ok("审核操作成功") :
                R.error("审核操作失败");
    }


}
